package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

public class UserRegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设定request请求的编码格式
        request.setCharacterEncoding("UTF-8");
        //响应数据给浏览器时，中文会不会乱码
        response.setContentType("text/html;charset=utf-8");
        //获取表单提交的请求参数
        //key(name)-value   当name对应的value是唯一时
        String user_name = request.getParameter("user_name");
        System.out.println("user_name = " + user_name);
        String user_psw = request.getParameter("user_psw");
        System.out.println("user_psw = " + user_psw);
        String gender = request.getParameter("gender");
        System.out.println("gender = " + gender);
        //当name对应的value可能不是唯一时,返回字符串数组
        String[] habits = request.getParameterValues("habit");
        System.out.println("habits = " + Arrays.toString(habits));
        //获取请求参数的name值列表
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            System.out.println("names.nextElement() = " + names.nextElement());
        }
        //获取请求参数的key-value键值对
        Map<String, String[]> map = request.getParameterMap();
        map.forEach((key, strs) -> {
            System.out.println("key = " + key);
            System.out.println("strs = " + Arrays.toString(strs));
        });

        response.getWriter().write("响应数据成功");

    }
}
